<?php

class Back_Model_Tickets extends Zend_Db_Table_Abstract
{
    protected $_name = 'ticket_themes_adv';
    
    /**
     * Выборка всех тем публикаторов
     * @return array 
     */
    public function getAllThemeAdvertiser() {
        $db = $this->getDefaultAdapter();
        $select = $db->select()
                ->from('ticket_themes_adv', array('theme', 'date_adv' => 'date', 
                    'id_status_theme_adv' => 'id_status_theme',
                    'id_theme' => 'id', 'id_adv' => 'id_advertiser')) 
                ->join(array('a' => 'advertisers'), 'a.id = id_advertiser', array('username' => 'a.login'))
                ->where('id_status_theme = 2');       
        return $db->fetchAll($select);
    }
    /**
     * Выборка всех тем рекламодателей
     * @return array 
     */
    public function getAllThemePublisher() {
        $db = $this->getDefaultAdapter();
        $select = $db->select()
                ->from('ticket_themes_pub',  array('theme', 'date_pub' => 'date', 
                    'id_status_theme_pub' => 'id_status_theme',
                    'id_theme' => 'id', 'id_pub' => 'id_publisher'))
                ->join(array('p' => 'publishers'), 'p.id = id_publisher', array('username' => 'p.login'))
                ->where('id_status_theme = 2');
        return $db->fetchAll($select);
    }
    
   public function getAllMessageAdvertiser($id_theme) {
       $select = $this->select()
               ->from('ticket_themes_adv', array('id', 'theme', 'id_advertiser'))
               ->join(array('a' => 'advertisers'), 'a.id = id_advertiser', array('username' => 'a.login'))
               ->join(array('tma' => 'ticket_messages_adv'), 'tma.id_ticket_theme_adv = ticket_themes_adv.id', 
                       array('message' => 'message', 'date_message' => 'date', 'id_user'))
               ->where('ticket_themes_adv.id = ?', intval($id_theme))
               ->order('date_message')
               ->setIntegrityCheck(false);
       return $this->fetchAll($select);
   }
   
   public function getAllMessagePublisher($id_theme) {
       $select = $this->select()
               ->from('ticket_themes_pub', array('id', 'theme', 'id_publisher'))
               ->join(array('p' => 'publishers'), 'p.id = id_publisher', array('username' => 'p.login'))
               ->join(array('tmp' => 'ticket_messages_pub'), 'tmp.id_ticket_theme_pub = ticket_themes_pub.id', 
                       array('message' => 'message', 'date_message' => 'date', 'id_user'))
               ->where('ticket_themes_pub.id = ?', intval($id_theme))
               ->order('date_message')
               ->setIntegrityCheck(false);
       return $this->fetchAll($select);
   }
   
  
}